home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / FOPEN.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  3KB  |  93 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    f o p e n . c                                                   */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <share.h>
  21. #include <io.h>
  22.  
  23. /*--------------------------------------------------------------------*/
  24. /*                    UUPC/extended include files                     */
  25. /*--------------------------------------------------------------------*/
  26.  
  27. #include "lib.h"
  28. #include "hlib.h"
  29. #include "ssleep.h"
  30.  
  31. #define SHARE_OPEN
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*    F O P E N                                                       */
  35. /*                                                                    */
  36. /*    Like fopen() but create imtermediate directories                */
  37. /*                                                                    */
  38. /*    This routine has dependency on the path separator characters    */
  39. /*    being '/', we should relove that somehow someday.               */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. FILE *FSOPEN(const char *name, const char *mode)
  43. {
  44.  
  45.    char *last;
  46.    FILE *results;
  47.  
  48.    /* are we opening for write or append */
  49.  
  50. #ifdef SHARE_OPEN
  51.    int share = SH_DENYWR;
  52.    int retries = 0;
  53.  
  54.    results = _fsopen(name, mode, share );
  55. #else
  56.    results = fopen(name, mode );
  57. #endif
  58.  
  59.    if ((results != nil(FILE)) || (*mode == 'r'))
  60.       return results;
  61.  
  62. /*--------------------------------------------------------------------*/
  63. /*       Verify all intermediate directories in the path exist        */
  64. /*--------------------------------------------------------------------*/
  65.  
  66.  
  67.    if ((last = strrchr(name, '/')) != nil(char))
  68.    {
  69.       *last = '\0';
  70.       MKDIR(name);
  71.       *last = '/';
  72.    }
  73.  
  74. /*--------------------------------------------------------------------*/
  75. /*                         Now try open again                         */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. #ifdef SHARE_OPEN
  79.    for ( ;; )
  80.    {
  81.       results = _fsopen(name, mode, share);
  82.       if (( results != NULL ) || (!bflag[ F_MULTITASK ]) ||
  83.           (errno != EACCES)   || (retries++ > 10))
  84.          return results;
  85.       perror( name );
  86.       ssleep( retries * 2);
  87.    }
  88. #else
  89.    return fopen(name, mode);
  90. #endif
  91.  
  92. } /*FOPEN*/
  93.